

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>toydata.Graph &mdash; ToyData 1.0 documentation</title>
  

  
  
  
  

  
  <script type="text/javascript" src="../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
        <script type="text/javascript" src="../../_static/jquery.js"></script>
        <script type="text/javascript" src="../../_static/underscore.js"></script>
        <script type="text/javascript" src="../../_static/doctools.js"></script>
        <script type="text/javascript" src="../../_static/language_data.js"></script>
    
    <script type="text/javascript" src="../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../index.html" class="icon icon-home"> ToyData
          

          
          </a>

          
            
            
              <div class="version">
                1.0
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">Contents:</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../Intro.html">Introductions</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../Overview.html">Overview</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../modules.html">toydata</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../index.html">ToyData</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../index.html">Module code</a> &raquo;</li>
        
      <li>toydata.Graph</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for toydata.Graph</h1><div class="highlight"><pre>
<span></span><span class="kn">from</span> <span class="nn">copy</span> <span class="kn">import</span> <span class="n">deepcopy</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Dict</span><span class="p">,</span> <span class="n">Union</span>


<div class="viewcode-block" id="Graph"><a class="viewcode-back" href="../../toydata.html#toydata.Graph.Graph">[docs]</a><span class="k">class</span> <span class="nc">Graph</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;Representation of a simple graph using an adjacency map.</span>
<span class="sd">    Adjacency Map Structure implemented with hash tables.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="c1"># nested Vertex class</span>
<div class="viewcode-block" id="Graph.Vertex"><a class="viewcode-back" href="../../toydata.html#toydata.Graph.Graph.Vertex">[docs]</a>    <span class="k">class</span> <span class="nc">Vertex</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;Lightweight vertex structure for a graph&quot;&quot;&quot;</span>
        <span class="vm">__slots__</span> <span class="o">=</span> <span class="s1">&#39;_element&#39;</span>

        <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span>
            <span class="sd">&quot;&quot;&quot;Do not call constructor directly. </span>
<span class="sd">            Use Graph&#39;s insert_vertex(x).&quot;&quot;&quot;</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_element</span> <span class="o">=</span> <span class="n">x</span>

        <span class="k">def</span> <span class="fm">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
            <span class="k">return</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_element</span><span class="p">)</span>
        
        <span class="k">def</span> <span class="fm">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
            <span class="k">return</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_element</span><span class="p">)</span>
        
<div class="viewcode-block" id="Graph.Vertex.element"><a class="viewcode-back" href="../../toydata.html#toydata.Graph.Graph.Vertex.element">[docs]</a>        <span class="k">def</span> <span class="nf">element</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
            <span class="sd">&quot;&quot;&quot;Return element associated with this vertex.&quot;&quot;&quot;</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_element</span></div>

        <span class="k">def</span> <span class="fm">__hash__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
            <span class="c1"># will allow vertex to be a map/set key</span>
            <span class="k">return</span> <span class="nb">hash</span><span class="p">(</span><span class="nb">id</span><span class="p">(</span><span class="bp">self</span><span class="p">))</span></div>

    <span class="c1"># nested Edge class</span>
<div class="viewcode-block" id="Graph.Edge"><a class="viewcode-back" href="../../toydata.html#toydata.Graph.Graph.Edge">[docs]</a>    <span class="k">class</span> <span class="nc">Edge</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;lightweight edge structure for a graph&quot;&quot;&quot;</span>
        <span class="vm">__slots__</span> <span class="o">=</span> <span class="s1">&#39;_origin&#39;</span><span class="p">,</span> <span class="s1">&#39;_destination&#39;</span><span class="p">,</span> <span class="s1">&#39;_element&#39;</span>

        <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">u</span><span class="p">,</span> <span class="n">v</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span>
            <span class="sd">&quot;&quot;&quot;Do not call constructor directly.</span>
<span class="sd">            Use Graph&#39;s insert_edge(u, v, x)&quot;&quot;&quot;</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_origin</span> <span class="o">=</span> <span class="n">u</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_destination</span> <span class="o">=</span> <span class="n">v</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_element</span> <span class="o">=</span> <span class="n">x</span>

        <span class="k">def</span> <span class="fm">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
            <span class="k">return</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{self._origin}</span><span class="s2">-(</span><span class="si">{self._element}</span><span class="s2">)-</span><span class="si">{self._destination}</span><span class="s2">&quot;</span>
        
        <span class="k">def</span> <span class="fm">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
            <span class="k">return</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{self._origin}</span><span class="s2">-(</span><span class="si">{self._element}</span><span class="s2">)-</span><span class="si">{self._destination}</span><span class="s2">&quot;</span>

<div class="viewcode-block" id="Graph.Edge.endpoint"><a class="viewcode-back" href="../../toydata.html#toydata.Graph.Graph.Edge.endpoint">[docs]</a>        <span class="k">def</span> <span class="nf">endpoint</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
            <span class="sd">&quot;&quot;&quot;Return (u, v) tuple for vertices u and v&quot;&quot;&quot;</span>
            <span class="k">return</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_origin</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_destination</span><span class="p">)</span></div>

<div class="viewcode-block" id="Graph.Edge.opposite"><a class="viewcode-back" href="../../toydata.html#toydata.Graph.Graph.Edge.opposite">[docs]</a>        <span class="k">def</span> <span class="nf">opposite</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">v</span><span class="p">):</span>
            <span class="sd">&quot;&quot;&quot;Return the vertex that is opposite v on the edge&quot;&quot;&quot;</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_destination</span> <span class="k">if</span> <span class="n">v</span> <span class="ow">is</span> <span class="bp">self</span><span class="o">.</span><span class="n">_origin</span> <span class="k">else</span> <span class="bp">self</span><span class="o">.</span><span class="n">_origin</span></div>

<div class="viewcode-block" id="Graph.Edge.element"><a class="viewcode-back" href="../../toydata.html#toydata.Graph.Graph.Edge.element">[docs]</a>        <span class="k">def</span> <span class="nf">element</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
            <span class="sd">&quot;&quot;&quot;Return element associated with this edge&quot;&quot;&quot;</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_element</span></div>
        
        <span class="k">def</span> <span class="fm">__hash__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
            <span class="c1"># will allow edge to be a map/set key</span>
            <span class="k">return</span> <span class="nb">hash</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">_origin</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_destination</span><span class="p">))</span></div>


    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">directed</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Create an empty graph(undirected by default)</span>
<span class="sd">        Graph is directed if optional parameter is set to True&quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_outgoing</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="c1"># only create second map for directed graph; use alias for undirected</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_incoming</span> <span class="o">=</span> <span class="p">{}</span> <span class="k">if</span> <span class="n">directed</span> <span class="k">else</span> <span class="bp">self</span><span class="o">.</span><span class="n">_outgoing</span>

<div class="viewcode-block" id="Graph.is_directed"><a class="viewcode-back" href="../../toydata.html#toydata.Graph.Graph.is_directed">[docs]</a>    <span class="k">def</span> <span class="nf">is_directed</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return True if this is a directed graph; False if undirected.</span>
<span class="sd">        Property is based on the original declaration of the graph,</span>
<span class="sd">        not its content&quot;&quot;&quot;</span>
        <span class="c1"># directed if maps are distinct</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_incoming</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_outgoing</span></div>

<div class="viewcode-block" id="Graph.vertex_count"><a class="viewcode-back" href="../../toydata.html#toydata.Graph.Graph.vertex_count">[docs]</a>    <span class="k">def</span> <span class="nf">vertex_count</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return the number of vertives in the graph&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_outgoing</span><span class="p">)</span></div>

<div class="viewcode-block" id="Graph.vertices"><a class="viewcode-back" href="../../toydata.html#toydata.Graph.Graph.vertices">[docs]</a>    <span class="k">def</span> <span class="nf">vertices</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return an iteration of all vertices of the graph&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="nb">list</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_outgoing</span><span class="o">.</span><span class="n">keys</span><span class="p">())</span></div>

<div class="viewcode-block" id="Graph.edge_count"><a class="viewcode-back" href="../../toydata.html#toydata.Graph.Graph.edge_count">[docs]</a>    <span class="k">def</span> <span class="nf">edge_count</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return the number of edges in the graph&quot;&quot;&quot;</span>
        <span class="n">total</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_outgoing</span><span class="p">[</span><span class="n">v</span><span class="p">])</span> <span class="k">for</span> <span class="n">v</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_outgoing</span><span class="p">)</span>
        <span class="c1"># for undirected graphs, make sure not to double-count edges&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">total</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_directed</span><span class="p">()</span> <span class="k">else</span> <span class="n">total</span> <span class="o">//</span> <span class="mi">2</span></div>

<div class="viewcode-block" id="Graph.edges"><a class="viewcode-back" href="../../toydata.html#toydata.Graph.Graph.edges">[docs]</a>    <span class="k">def</span> <span class="nf">edges</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return a set of all edges of the graph&quot;&quot;&quot;</span>
        <span class="c1"># avoid double-reporting edges of undirected graph</span>
        <span class="n">result</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">secondary_map</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_outgoing</span><span class="o">.</span><span class="n">values</span><span class="p">():</span>
            <span class="c1"># add edges to resulting set</span>
            <span class="n">result</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">secondary_map</span><span class="o">.</span><span class="n">values</span><span class="p">())</span>
        <span class="k">return</span> <span class="n">result</span></div>

<div class="viewcode-block" id="Graph.get_edge"><a class="viewcode-back" href="../../toydata.html#toydata.Graph.Graph.get_edge">[docs]</a>    <span class="k">def</span> <span class="nf">get_edge</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">u</span><span class="p">,</span> <span class="n">v</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return the edge from u to v, or None if not adjacent&quot;&quot;&quot;</span>
        <span class="c1"># return None if v not adjacent</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_outgoing</span><span class="p">[</span><span class="n">u</span><span class="p">]</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">v</span><span class="p">)</span></div>

<div class="viewcode-block" id="Graph.degree"><a class="viewcode-back" href="../../toydata.html#toydata.Graph.Graph.degree">[docs]</a>    <span class="k">def</span> <span class="nf">degree</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">v</span><span class="p">,</span> <span class="n">outgoing</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return number of (outgoing) edges incident to vertex v</span>
<span class="sd">        in the graph. If graph is directed, optional parameter </span>
<span class="sd">        used to count incoming edges&quot;&quot;&quot;</span>

        <span class="n">adj</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_outgoing</span> <span class="k">if</span> <span class="n">outgoing</span> <span class="k">else</span> <span class="bp">self</span><span class="o">.</span><span class="n">_incoming</span>
        <span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="n">adj</span><span class="p">[</span><span class="n">v</span><span class="p">])</span></div>

<div class="viewcode-block" id="Graph.incident_edges"><a class="viewcode-back" href="../../toydata.html#toydata.Graph.Graph.incident_edges">[docs]</a>    <span class="k">def</span> <span class="nf">incident_edges</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">v</span><span class="p">,</span> <span class="n">outgoing</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return all(outgoing) edges incident to vertex v in</span>
<span class="sd">        the graph. If graph is directed, optional parameter used</span>
<span class="sd">        to request incoming edges&quot;&quot;&quot;</span>
        <span class="n">adj</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_outgoing</span> <span class="k">if</span> <span class="n">outgoing</span> <span class="k">else</span> <span class="bp">self</span><span class="o">.</span><span class="n">_incoming</span>
        <span class="k">for</span> <span class="n">edges</span> <span class="ow">in</span> <span class="n">adj</span><span class="p">[</span><span class="n">v</span><span class="p">]</span><span class="o">.</span><span class="n">values</span><span class="p">():</span>
            <span class="k">yield</span> <span class="n">edges</span></div>
        
<div class="viewcode-block" id="Graph.insert_vertex"><a class="viewcode-back" href="../../toydata.html#toydata.Graph.Graph.insert_vertex">[docs]</a>    <span class="k">def</span> <span class="nf">insert_vertex</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Insert and return a new Vertex with element x&quot;&quot;&quot;</span>
        <span class="n">v</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">Vertex</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_outgoing</span><span class="p">[</span><span class="n">v</span><span class="p">]</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_directed</span><span class="p">():</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_incoming</span><span class="p">[</span><span class="n">v</span><span class="p">]</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">return</span> <span class="n">v</span></div>
    
<div class="viewcode-block" id="Graph.insert_edge"><a class="viewcode-back" href="../../toydata.html#toydata.Graph.Graph.insert_edge">[docs]</a>    <span class="k">def</span> <span class="nf">insert_edge</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">u</span><span class="p">,</span> <span class="n">v</span><span class="p">,</span> <span class="n">x</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Insert and return a new Edge from u to v with auxliary element x&quot;&quot;&quot;</span>
        <span class="n">e</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">Edge</span><span class="p">(</span><span class="n">u</span><span class="p">,</span> <span class="n">v</span><span class="p">,</span> <span class="n">x</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_outgoing</span><span class="p">[</span><span class="n">u</span><span class="p">][</span><span class="n">v</span><span class="p">]</span> <span class="o">=</span> <span class="n">e</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_incoming</span><span class="p">[</span><span class="n">v</span><span class="p">][</span><span class="n">u</span><span class="p">]</span> <span class="o">=</span> <span class="n">e</span>
        <span class="k">return</span> <span class="n">e</span></div>
    
<div class="viewcode-block" id="Graph.remove_vertex"><a class="viewcode-back" href="../../toydata.html#toydata.Graph.Graph.remove_vertex">[docs]</a>    <span class="k">def</span> <span class="nf">remove_vertex</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">v</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;InserDeltet and return the Vertex v</span>
<span class="sd">        Raise KeyError is v not in graph.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">v</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_outgoing</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">KeyError</span><span class="p">(</span><span class="s1">&#39;Key Error &#39;</span> <span class="o">+</span> <span class="nb">repr</span><span class="p">(</span><span class="n">v</span><span class="p">))</span>
        <span class="k">del</span> <span class="bp">self</span><span class="o">.</span><span class="n">_outgoing</span><span class="p">[</span><span class="n">v</span><span class="p">]</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_directed</span><span class="p">():</span>
            <span class="k">del</span> <span class="bp">self</span><span class="o">.</span><span class="n">_incoming</span><span class="p">[</span><span class="n">v</span><span class="p">]</span>
        <span class="k">return</span> <span class="n">v</span></div>

<div class="viewcode-block" id="Graph.remove_edges"><a class="viewcode-back" href="../../toydata.html#toydata.Graph.Graph.remove_edges">[docs]</a>    <span class="k">def</span> <span class="nf">remove_edges</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">e</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Delete adn return edge e from graph&quot;&quot;&quot;</span>
        <span class="n">u</span><span class="p">,</span> <span class="n">v</span> <span class="o">=</span> <span class="n">e</span><span class="o">.</span><span class="n">_origin</span><span class="p">,</span> <span class="n">e</span><span class="o">.</span><span class="n">_destination</span>
        <span class="k">del</span> <span class="bp">self</span><span class="o">.</span><span class="n">_outgoing</span><span class="p">[</span><span class="n">u</span><span class="p">][</span><span class="n">v</span><span class="p">]</span>
        <span class="k">del</span> <span class="bp">self</span><span class="o">.</span><span class="n">_incoming</span><span class="p">[</span><span class="n">v</span><span class="p">][</span><span class="n">u</span><span class="p">]</span>
        <span class="k">return</span> <span class="n">e</span></div>

<div class="viewcode-block" id="Graph.dfs"><a class="viewcode-back" href="../../toydata.html#toydata.Graph.Graph.dfs">[docs]</a>    <span class="k">def</span> <span class="nf">dfs</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">u</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Perform DFS of the undiscovered portion of Graph g</span>
<span class="sd">        starting at Vertex u.</span>

<span class="sd">        Discovered is a dictionary mapping each vertex to the</span>
<span class="sd">        edge that was used to discover it during the DFS(u should</span>
<span class="sd">        be &quot;discovered&quot; prior to the call.)Newly discovered vertices</span>
<span class="sd">        will be added to the dictionary as a result.&quot;&quot;&quot;</span>

        <span class="c1"># typing of discovered</span>
        <span class="n">VE</span> <span class="o">=</span> <span class="n">Dict</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">Vertex</span><span class="p">,</span> <span class="n">Union</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">Edge</span><span class="p">,</span> <span class="kc">None</span><span class="p">]]</span>
        <span class="c1"># here we use closure to save dfs path</span>
        <span class="c1"># with u trivially discovered</span>
        <span class="n">discovered</span><span class="p">:</span> <span class="n">VE</span> <span class="o">=</span> <span class="p">{</span><span class="n">u</span><span class="p">:</span> <span class="kc">None</span><span class="p">}</span>
        <span class="c1"># traverse the graph</span>
        <span class="k">def</span> <span class="nf">_dfs</span><span class="p">(</span><span class="n">u</span><span class="p">):</span>
            <span class="c1"># for every outgoing edge from u</span>
            <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">incident_edges</span><span class="p">(</span><span class="n">u</span><span class="p">):</span>
                <span class="n">v</span> <span class="o">=</span> <span class="n">e</span><span class="o">.</span><span class="n">opposite</span><span class="p">(</span><span class="n">u</span><span class="p">)</span>
                <span class="c1"># v is an unvisited vertex</span>
                <span class="k">if</span> <span class="n">v</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">discovered</span><span class="p">:</span>
                    <span class="c1"># e is the tree edge that discovered v</span>
                    <span class="n">discovered</span><span class="p">[</span><span class="n">v</span><span class="p">]</span> <span class="o">=</span> <span class="n">e</span>
                    <span class="c1"># recursively explore from v</span>
                    <span class="n">_dfs</span><span class="p">(</span><span class="n">v</span><span class="p">)</span>
        <span class="c1"># call it</span>
        <span class="n">_dfs</span><span class="p">(</span><span class="n">u</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">discovered</span></div>
        
    <span class="c1"># Reconstructing a Path from u to v</span>
<div class="viewcode-block" id="Graph.construct_path"><a class="viewcode-back" href="../../toydata.html#toydata.Graph.Graph.construct_path">[docs]</a>    <span class="k">def</span> <span class="nf">construct_path</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">u</span><span class="p">,</span> <span class="n">v</span><span class="p">,</span> <span class="n">dfs</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">dfs</span><span class="p">:</span>
            <span class="n">discovered</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">dfs</span><span class="p">(</span><span class="n">u</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">discovered</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">bfs</span><span class="p">(</span><span class="n">u</span><span class="p">)</span>
        <span class="c1"># empty path by default</span>
        <span class="n">path</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">if</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">discovered</span><span class="p">:</span>
            <span class="c1"># we build list from v to u and then revese it at the end</span>
            <span class="n">path</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">v</span><span class="p">)</span>
            <span class="n">walk</span> <span class="o">=</span> <span class="n">v</span>
            <span class="k">while</span> <span class="n">walk</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">u</span><span class="p">:</span>
                <span class="n">e</span> <span class="o">=</span> <span class="n">discovered</span><span class="p">[</span><span class="n">walk</span><span class="p">]</span>
                <span class="n">parent</span> <span class="o">=</span> <span class="n">e</span><span class="o">.</span><span class="n">opposite</span><span class="p">(</span><span class="n">walk</span><span class="p">)</span>
                <span class="n">path</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">parent</span><span class="p">)</span>
                <span class="n">walk</span> <span class="o">=</span> <span class="n">parent</span>
            <span class="c1"># rotate path from u to v</span>
            <span class="n">path</span><span class="o">.</span><span class="n">reverse</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">path</span></div>

<div class="viewcode-block" id="Graph.dfs_complete"><a class="viewcode-back" href="../../toydata.html#toydata.Graph.Graph.dfs_complete">[docs]</a>    <span class="k">def</span> <span class="nf">dfs_complete</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Perform DFS for entire graph and return forest as a dictionary.</span>

<span class="sd">        Result maps each vertex v tp the edge that was used to discover it.</span>
<span class="sd">        (Vertices that are roots of a DFS tree are mapped to None)&quot;&quot;&quot;</span>
        <span class="n">forest</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">for</span> <span class="n">u</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">vertices</span><span class="p">():</span>
            <span class="k">if</span> <span class="n">u</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">forest</span><span class="p">:</span>
                <span class="c1"># u will be the root of a tree</span>
                <span class="n">forest</span><span class="p">[</span><span class="n">u</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">dfs</span><span class="p">(</span><span class="n">u</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">forest</span></div>

<div class="viewcode-block" id="Graph.bfs"><a class="viewcode-back" href="../../toydata.html#toydata.Graph.Graph.bfs">[docs]</a>    <span class="k">def</span> <span class="nf">bfs</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">s</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Perform BFS of the undiscovered portion of Graph g starting at</span>
<span class="sd">        Vertex s.</span>

<span class="sd">        discovered is a dictionary mapping each vertex to the edge that </span>
<span class="sd">        was used to discover it during the BFS(s should be mapped to None</span>
<span class="sd">        prior to the call). Newly discovered vertices will be added to the</span>
<span class="sd">        dictionary as a result.&quot;&quot;&quot;</span>
        <span class="n">discovered</span> <span class="o">=</span> <span class="p">{</span><span class="n">s</span><span class="p">:</span> <span class="kc">None</span><span class="p">}</span>
        <span class="c1"># first level includes only s</span>
        <span class="n">level</span> <span class="o">=</span> <span class="p">[</span><span class="n">s</span><span class="p">]</span>
        <span class="k">while</span> <span class="nb">len</span><span class="p">(</span><span class="n">level</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
            <span class="c1"># prepateto gather newly found vertices</span>
            <span class="n">next_level</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="k">for</span> <span class="n">u</span> <span class="ow">in</span> <span class="n">level</span><span class="p">:</span>
                <span class="c1"># for every outgoing edge from u</span>
                <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">incident_edges</span><span class="p">(</span><span class="n">u</span><span class="p">):</span>
                    <span class="n">v</span> <span class="o">=</span> <span class="n">e</span><span class="o">.</span><span class="n">opposite</span><span class="p">(</span><span class="n">u</span><span class="p">)</span>
                    <span class="c1"># v is an unvisited vertex</span>
                    <span class="k">if</span> <span class="n">v</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">discovered</span><span class="p">:</span>
                        <span class="c1"># e is the edge that discovered v</span>
                        <span class="n">discovered</span><span class="p">[</span><span class="n">v</span><span class="p">]</span> <span class="o">=</span> <span class="n">e</span>
                        <span class="c1"># v will be further considered in next pass</span>
                        <span class="n">next_level</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">v</span><span class="p">)</span>
            <span class="c1"># relabel &#39;next&#39; level to become current</span>
            <span class="n">level</span> <span class="o">=</span> <span class="n">next_level</span>
        <span class="k">return</span> <span class="n">discovered</span></div>

    <span class="c1"># Floyd-Warshall algorithm</span>
<div class="viewcode-block" id="Graph.floyd_warshall"><a class="viewcode-back" href="../../toydata.html#toydata.Graph.Graph.floyd_warshall">[docs]</a>    <span class="k">def</span> <span class="nf">floyd_warshall</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return a new graph that is the transitive closure of g&quot;&quot;&quot;</span>
        <span class="n">closure</span> <span class="o">=</span> <span class="n">deepcopy</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
        <span class="c1"># make indexable list</span>
        <span class="n">verts</span> <span class="o">=</span> <span class="n">closure</span><span class="o">.</span><span class="n">vertices</span><span class="p">()</span>
        <span class="n">n</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">verts</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
            <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
                <span class="c1"># verify that edge (i, k) exists in the partial closure</span>
                <span class="k">if</span> <span class="n">i</span> <span class="o">!=</span> <span class="n">k</span> <span class="ow">and</span> <span class="n">closure</span><span class="o">.</span><span class="n">get_edge</span><span class="p">(</span><span class="n">verts</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">verts</span><span class="p">[</span><span class="n">k</span><span class="p">])</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                    <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
                        <span class="c1"># verify that edge(k, j) exists in the partial closure</span>
                        <span class="k">if</span> <span class="n">i</span> <span class="o">!=</span> <span class="n">j</span> <span class="o">!=</span> <span class="n">k</span> <span class="ow">and</span> <span class="n">closure</span><span class="o">.</span><span class="n">get_edge</span><span class="p">(</span><span class="n">verts</span><span class="p">[</span><span class="n">k</span><span class="p">],</span> <span class="n">verts</span><span class="p">[</span><span class="n">j</span><span class="p">]):</span>
                            <span class="c1"># if (i, j) not yet included, add it to the closure</span>
                            <span class="k">if</span> <span class="n">closure</span><span class="o">.</span><span class="n">get_edge</span><span class="p">(</span><span class="n">verts</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">verts</span><span class="p">[</span><span class="n">j</span><span class="p">])</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                                <span class="n">closure</span><span class="o">.</span><span class="n">insert_edge</span><span class="p">(</span><span class="n">verts</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">verts</span><span class="p">[</span><span class="n">j</span><span class="p">])</span></div></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2019, Xiangzhuang Shen

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>